/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author BENSON
 */
public class ControllerShowImports {
    
      /**
     * Variable declarations
     * Object declarations
    */
    Connection connect = null;
    String list;
    Statement stmt = null;
    String path="";
    File file=null;
    public Views.ShowImport  showImpo;
    private ActionListener actionListener;
    Scanner reader=null;

     /**
     * Constructor for instantiating declared objects
     */
public ControllerShowImports(){
        
        showImpo = new Views.ShowImport();
        
        showAllImports();
}

 /**
  * Specifies the location of the file to be imported-read
  */

private void readReport() {
    
    JFrame parentFrame = new JFrame();
 
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("CHOOSE FILE TO IMPORT");   
 
    int userSelection = fileChooser.showSaveDialog(parentFrame);
 
    if (userSelection == JFileChooser.APPROVE_OPTION) {
        File fileToImport = fileChooser.getSelectedFile();
        path=fileToImport.getAbsolutePath();
    }
    file = new File(path);
}
 /**
  * Reads and shows the read file in a list model
  */   

public void showAllImports(){
        try{
            readReport();
            reader= new Scanner(file);
            showImpo.setVisible(true);
        }
        catch(FileNotFoundException e){
            JOptionPane.showMessageDialog(null,e.toString());
        }
        while(reader.hasNextLine()){
                
                showImpo.getImportList().addElement(reader.nextLine());
        }
                
          
}
    
    
/**
 * Sets actionListeners to the buttons
 */

public void control(){        
        actionListener = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {  
                  if(actionEvent.getSource()==showImpo.getCloseBtn()){
                  showImpo.setVisible(false);
                  }
              }
              
        };                
        showImpo.getCloseBtn().addActionListener(actionListener);
}
     
    
}
